/*
 * CMapNode.h
 *
 *      Author: grifos
 */

#ifndef CMAPNODE_H_
#define CMAPNODE_H_
//OSG  includes
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Quat>
#include <osg/Matrix>
#include <osg/ShapeDrawable>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Transform>
#include <osg/Material>
#include <osg/NodeCallback>
#include <osg/Depth>
#include <osg/CullFace>
#include <osg/TexMat>
#include <osg/TexGen>
#include <osg/TexEnv>
#include <osg/TexEnvCombine>
#include <osg/TextureCubeMap>
#include <osg/VertexProgram>
#include <osg/LineSegment>

#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>

#include <osgDB/ReadFile>
#include <osg/Texture2D>
#include <osgUtil/IntersectVisitor>
#include <CSkyBox.h>
#include <CJoueur.h>
#include <CObject3d.h>
#include <CArbre.h>
#include <iostream>

class CMapNode {
private:
	osg::ref_ptr<osg::MatrixTransform> _root;
	osg::ref_ptr<osg::MatrixTransform> _rootRot0;
	osg::ref_ptr<osg::MatrixTransform> _rootRot1;
	osg::ref_ptr<osg::Geode> _map;
	osg::ref_ptr<osg::Group> _bombe;
	osg::ref_ptr<osg::Group> _elementOnmap;

	void createMapNode(std::string heightFile, std::string texFile);
	osg::Vec3d calculNormal(osg::Vec3d x, osg::Vec3d y, osg::Vec3d z);

	float _phi;
	float _theta;
	float _omega;

	CJoueur * _joueur; // joueur attaché à la map
	CSkyBox * _skyBox;

	CArbre* _treeElement;
	CObject3d* _rockElement;
	CObject3d* _chaletElement;

	void calculColision();
	float fabs(float v){ return ((v>=0)?v:-v);}

public:
	CMapNode(CJoueur * j,CSkyBox *s);
	virtual ~CMapNode();

	osg::Node* getNode(){ return _root.get(); }
	osg::Geode* getMap(){ return _map.get(); }

	void update(float delta);
	osg::Vec3d calculHeight(osg::Vec3d pos);
	void addBomb(osg::Node* b){ _bombe->addChild(b); }
	void removeBomb(){ _bombe->removeChildren(0,_bombe->getNumChildren()); }
	void addElementOnMap(float posx, float posy, int element);
	osg::Group* getBombGroup(){ return _bombe.get(); }

};

#endif /* CMAPNODE_H_ */
